const app = getApp();
const util = require('../../utils/util.js')

Page({
    data: {
        inputShowed: false,
        inputVal: "",
        name: '',
        username:'',
        password:'',
        prompt: '',
        noResult: false,
        users: [],
        index:'',
        isAdd:false,

    },
    onLoad: function (option) {
        wx.setNavigationBarTitle({
            title: '用户管理'
        })
        console.log(option);
        this.getUserList('');
    },
    onShow: function () {
        this.getUserList('');
    },
    showInput: function () {
        this.setData({
            inputShowed: true
        });
    },
    hideInput: function () {
        this.setData({
            inputVal: "",
            inputShowed: false,
            isAdd: false,
        });
        this.getUserList('');
    },
    clearInput: function () {
        this.setData({
            inputVal: ""
        });
    },
    inputTyping: function (e) {

        this.setData({
            inputVal: e.detail.value,
        })
        this.getUserList(this.data.inputVal);
        // this.getAreaList(this.data.longitude,this.data.latitude,this.data.inputVal)

    },
    getUserList: function (inputValue) {
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/admin/user/getUserFast',
            data: {
                token: app.globalData.token,
                searchStr: inputValue,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {

                    if (res.data.records.list.length == 0) {
                        that.setData({
                            prompt: '没有找到相关用户,请确认输入正确',
                            noResult: true,
                            users: []
                        })
                    } else {
                        console.log(that.data.users)
                        that.setData({
                            users: res.data.records.list,
                            noResult: false
                        })
                        console.log(that.data.users)
                    }
                }
            },
            fail: function (err) {
                console.log(err)
            }
        })
    },
    selected: function (e) {
        var userId = e.currentTarget.dataset.id;
        var userInfos = this.data.users.filter(function (item) {
            return item.id == userId;
        })
        delete userInfos[0].password
        let userInfo = JSON.stringify(userInfos[0]);
        console.log(userInfo);
        wx.navigateTo({
            url: '/pages/user-detail/user-detail?userInfo=' + userInfo
        })
    },
    name: function (e) {
        this.setData({
            name:e.detail.value
        })
    },
    username: function (e) {
        this.setData({
            username:e.detail.value
        })
    },
    password: function (e) {
        this.setData({
            password:e.detail.value
        })
    },
    bindAddUser: function () {
        this.setData({
            isAdd: true
        })
    },
    cancelAdd: function () {
        this.setData({
            isAdd: false,
          username:"",
          name:"",
          password:""
        })
    },
    saveAdd: function () {
        console.log(this.data.username)
        wx.showLoading({
            title: '保存中'
        })
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/admin/user/save',
            data: {
                token: app.globalData.token,
                name: that.data.name,
                username: that.data.username,
                password: that.data.password,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    wx.showToast({
                        title: '添加成功',
                        icon: 'success',
                        duration: 3000
                    });
                  that.setData({
                    inputShowed: false,
                    isAdd: false,
                    username: "",
                    name: "",
                    password: ""
                  })
                  that.getUserList('');
                }
            },
            fail: function (err) {
                console.log(err)
            }
        })
    }
});